package cn.hxzy.service;


import cn.hxzy.dao.NewsDao;
import cn.hxzy.entity.News;
import cn.hxzy.entity.User;
import cn.hxzy.entity.vo.PageObj;

import java.sql.SQLException;
import java.util.List;

public class NewsService {
    NewsDao newsDao = new NewsDao();

    /**
     * 前台显示新闻页面
     */
    public List<News> findAll() throws Exception {
        return newsDao.findAllWithUser();
    }

    /**
     * 携带页面排序的查找全部
     **/
    public PageObj<News> findAll(PageObj<News> pageObj) throws Exception {
        List<News> all = newsDao.findAllWithUserAll(pageObj.getPage(), pageObj.getSize());
        System.out.println("*************\nshowNews的findAll的all为：\n" + all + "\n*************");
        pageObj.setList(all);
        long count = newsDao.countAll();
        pageObj.setCount(count);
        Integer size = pageObj.getSize();
        long l = count % size != 0 ? (count / size) + 1 : (count / size);
        pageObj.setPageCount((int) l);
        return pageObj;
    }


    /**
     * 前台新闻显示
     */
    public News findByIdWithUser(int id) throws Exception {
        return newsDao.findByIdWithUser(id);
    }

    public void add(News news) throws Exception {
        newsDao.add(news);
    }

    public void delete(int id) throws Exception {
        newsDao.delete(id);
    }

    public void update(News news) throws Exception {
        newsDao.update(news);
    }

    public News findByNewsNumber(int id) throws Exception {
        List<News> news = newsDao.findAll();
        for (News news1 : news) {
            if (news1.getId().equals(id)) {
                return news1;
            }
        }
        return null;
    }

    /**
     * 查询新闻id分页
     */
    public PageObj<News> findByIdWithUserName(PageObj<News> page, int id) throws Exception {
        try {
            System.out.println(id);
            List<News> all = newsDao.findByIdLike(page.getPage(), page.getSize(), id);
            page.setList(all);
            long count = newsDao.countAllWithId(id);
            page.setCount(count);
            Integer size = page.getSize();
            long l = count % size != 0 ? (count / size) + 1 : (count / size);
            page.setPageCount((int) l);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 查询新闻名字分页
     */
    public PageObj<News> findByNameWithUser(PageObj<News> page, String name) throws Exception {
        try {
            System.out.println(name);
            List<News> all = newsDao.findByNameLike(page.getPage(), page.getSize(), name);
            page.setList(all);
            System.out.println("*****************\n"+all+"\n*******************");
            long count = newsDao.countAllWithUser(name);
            page.setCount(count);
            Integer size = page.getSize();
            long l = count % size != 0 ? (count / size) + 1 : (count / size);
            page.setPageCount((int) l);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 查询新闻时间分页
     */
    public PageObj<News> findByDateWithUserDate(PageObj<News> page, String date) throws SQLException {
        try {
            System.out.println(date);
            List<News> all = newsDao.findByDateLike(page.getPage(), page.getSize(), date);
            page.setList(all);
            long count = newsDao.countAllWithDate(date);
            page.setCount(count);
            Integer size = page.getSize();
            long l = count % size != 0 ? (count / size) + 1 : (count / size);
            page.setPageCount((int) l);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    public PageObj<News> findByUserNameWithUser(PageObj<News> page, String users) {
        try {
            System.out.println(users);
            List<News> all = newsDao.findByUserNameLike(page.getPage(), page.getSize(), users);
            page.setList(all);
            long count = newsDao.countAllWithUserName(users);
            page.setCount(count);
            Integer size = page.getSize();
            long l = count % size != 0 ? (count / size) + 1 : (count / size);
            page.setPageCount((int) l);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }
}
